<?php
/**
 * [商网通 System] Copyright swtshop.cn
 * QQ: 1170787711
 * Author: gj.name
 * This is NOT a freeware
*/
defined('IN_UQ') or exit('Access Denied');
class api_uqcms extends control
{
    function __construct()
    {
        $this->aid = $_SESSION['admin']['aid'];
        parent::__construct();
    }
    function sms()
    {
        $data = $this->db->get_all("select * from " . table('api_sms') . " order by id asc");
        $this->assign('data', $data);
        $this->display();
    }
    function sms_edit()
    {
        $id = $_GET['id'] ? $_GET['id'] : showmsg('提交错误');
        $row = $this->db->get_one("select * from " . table('api_sms') . " where id = " . $id . " order by id desc");
        $this->assign('row', $row);
        $this->display();
    }
    function sms_save()
    {
        $setarr['name'] = P('name', '名称不能为空');
        $setarr['describe'] = P('describe');
        $setarr['app_key'] = P('app_key', 'APP KEY不能为空');
        $setarr['app_secret'] = P('app_secret', 'APP SECRET不能为空');
        $setarr['sign'] = P('sign', 'sign不能为空');
        $id = P('id', '提交错误');
        $query = $this->db->update(table('api_sms'), $setarr, 'id = ' . $id);
        if ($query) {
            right_json();
        } else {
            error_json('保存失败');
        }
    }
    function sms_ch()
    {
        $id = isset($_GET['id']) ? $_GET['id'] : showmsg('选择错误');
        $this->db->query("update " . table('api_sms') . " set status = 0");
        $query = $this->db->query("update " . table('api_sms') . " set status = 1 where id = " . $id);
        if ($query) {
            showmsg('设置成功');
        } else {
            showmsg('操作错误');
        }
    }
    function oss()
    {
        $data = $this->db->get_all("select * from " . table('api_oss') . " order by id asc");
        $this->assign('data', $data);
        $this->display();
    }
    function oss_edit()
    {
        $id = $_GET['id'] ? $_GET['id'] : showmsg('提交错误');
        $row = $this->db->get_one("select * from " . table('api_oss') . " where id = " . $id . " order by id desc");
        if ($row['param']) {
            $row['param'] = explode(',', $row['param']);
        }
        $row['pcon'] = unserialize($row['pcon']);
        $this->assign('row', $row);
        if ($row['alias'] != 'local') {
            if (!extension_loaded('sockets')) {
                $this->assign('error_status', true);
            }
        }
        $this->display();
    }
    function oss_save()
    {
        $setarr['name'] = P('name', '名称不能为空');
        $setarr['describe'] = P('describe');
        $setarr['bucket'] = P('bucket', 'BUCKET不能为空');
        $setarr['host'] = P('host');
        $setarr['https'] = P('https');
        $setarr['pcon'] = serialize(@$_POST['param']);
        $id = P('id', false);
        $query = $this->db->update(table('api_oss'), $setarr, 'id = ' . $id);
        if ($query) {
            right_json();
        } else {
            error_json('保存失败');
        }
    }
    function oss_ch()
    {
        $id = isset($_GET['id']) ? $_GET['id'] : showmsg('选择错误');
        $this->db->query("update " . table('api_oss') . " set status = 0");
        $query = $this->db->query("update " . table('api_oss') . " set status = 1 where id = " . $id);
        if ($query) {
            showmsg('设置成功');
        } else {
            showmsg('操作错误');
        }
    }
    function payment()
    {
        $data = $this->db->get_all("select * from " . table('api_payment'));
        $this->assign('data', $data);
        $this->display();
    }
    function payment_edit()
    {
        $id = $_GET['id'] ? $_GET['id'] : exit('ID参数错误！');
        $row = $this->db->get_one("select * from " . table('api_payment') . " where id = " . $id);
        if ($row['param']) {
            $row['param'] = explode(',', $row['param']);
        }
        $row['config'] = unserialize($row['config']);
        $this->assign('row', $row);
        $this->display();
    }
    public function payment_save()
    {
        if ($_POST) {
            $data['fees'] = P('fees', '', '0');
            $data['describe'] = P('describe');
            $data['status'] = P('status', '', '0');
            $data['config'] = serialize($_POST['param']);
            $id = P('id', '提交错误');
            $query = $this->db->update(table('api_payment'), $data, 'id = ' . $id);
            if ($query) {
                right_json();
            } else {
                error_json('保存失败');
            }
        }
    }
    function login()
    {
        $data = $this->db->get_all("select * from " . table('api_login') . " order by id asc");
        $this->assign('data', $data);
        $this->display();
    }
    function login_edit()
    {
        $id = $_GET['id'] ? $_GET['id'] : showmsg('提交错误');
        $row = $this->db->get_one("select * from " . table('api_login') . " where id = " . $id . " order by id desc");
        $this->assign('row', $row);
        $this->display();
    }
    function login_save()
    {
        $setarr['name'] = P('name', '名称不能为空');
        $setarr['describe'] = P('describe', '');
        $setarr['app_key'] = P('app_key', 'APPKEY不能为空');
        $setarr['app_secret'] = P('app_secret', 'APPSECRET不能为空');
        $setarr['status'] = P('status', '', '0');
        $id = P('id', false);
        $query = $this->db->update(table('api_login'), $setarr, 'id = ' . $id);
        if ($query) {
            right_json();
        } else {
            error_json('保存失败');
        }
    }
    function notice()
    {
        $data = $this->db->get_all("select * from " . table('api_notice') . " order by id asc");
        $this->assign('data', $data);
        $this->display();
    }
    function notice_type_edit()
    {
        $id = isset($_GET['id']) ? $_GET['id'] : false;
        if ($id) {
            $row = $this->db->get_one("select * from " . table('api_notice') . " where id = " . $id . " limit 1");
            $this->assign('row', $row);
        }
        $this->display();
    }
    function notice_type_save()
    {
        if ($_POST) {
            $setarr['name'] = P('name', '姓名不能为空');
            $setarr['alias'] = P('alias', '标签不能为空');
            $setarr['param'] = P('param', '参数不能为空');
            $setarr['status'] = P('status', '', '0');
            $id = P('id', 0);
            if ($id) {
                $query = $this->db->update(table('api_notice'), $setarr, 'id = ' . $id);
            } else {
                $query = $this->db->add(table('api_notice'), $setarr);
            }
            if ($query) {
                right_json();
            } else {
                error_json('保存失败');
            }
        } else {
            error_json('提交错误');
        }
    }
    function notice_tpl()
    {
        $type = isset($_GET['type']) ? $_GET['type'] : '';
        $alias = isset($_GET['alias']) ? $_GET['alias'] : '';
        if ($type) {
            if (!empty($alias)) {
                $wsql = " and type2 = '" . $alias . "' ";
            } else {
                $wsql = '';
            }
            $data_tpl = $this->db->get_all("select * from " . table('api_notice_tpl') . " where type = '" . $type . "' " . $wsql . " order by id asc");
            $notice_arr = $this->db->get_all("select * from " . table('api_notice'));
            if ($data_tpl) {
                foreach ($notice_arr as $k => $v) {
                    $notice_result[$v['id']] = $v['name'];
                }
                foreach ($data_tpl as $k => $v) {
                    $v['notice_name'] = $notice_result[$v['nid']];
                    $data[] = $v;
                }
                $this->assign('data', $data);
            }
        }
        $this->display();
    }
    function notice_tpl_edit()
    {
        $id = $_GET['id'] ? $_GET['id'] : showmsg('提交错误');
        $row = $this->db->get_one("select * from " . table('api_notice_tpl') . " where id = " . $id . " order by id desc");
        $this->assign('row', $row);
        $notice_tag = $this->db->get_all("select * from " . table('api_notice') . " where status = 1");
        $this->assign('notice_tag', $notice_tag);
        $this->display();
    }
    function notice_tpl_save()
    {
        $setarr['name'] = P('name', '名称不能为空');
        $setarr['tpl_id'] = P('tpl_id');
        $setarr['param'] = P('param', '内部参数不能为空');
        $setarr['param2'] = P('param2', '对外接口不能为空');
        $setarr['content'] = P('content', '模板内容不能为空');
        $setarr['status'] = P('status', '', '0');
        $id = P('id', 0);
        $query = $this->db->update(table('api_notice_tpl'), $setarr, 'id = ' . $id);
        if ($query) {
            right_json();
        } else {
            error_json('保存失败');
        }
    }
    function notice_tpl_test()
    {
        $id = $_GET['id'] ? $_GET['id'] : showmsg('提交错误');
        $row = $this->db->get_one("select * from " . table('api_notice') . " where id = " . $id);
        $this->assign('row', $row);
        $this->display();
    }
    function notice_log()
    {
        $type = isset($_GET['type']) ? $_GET['type'] : '';
        $page = isset($_GET['page']) ? $_GET['page'] : '1';
        $page_size = isset($_GET['page_size']) ? $_GET['page_size'] : '20';
        $wsql = '';
        if (!empty($type)) {
            $wsql .= ' where type =  ' . $type;
        }
        $all = $this->db->get_all("select * from " . table('notice') . $wsql . " order by id desc limit " . ($page - 1) * $page_size . "," . $page_size);
        if ($all) {
            foreach ($all as $k => $v) {
                $v['addtime'] = _date($v['addtime']);
                $list[] = $v;
            }
            $data['list'] = $list;
            $total = $this->db->get_one("select count(id) as total from " . table('notice') . $wsql);
            if ($total['total'] > $page_size) {
                $data['page'] = $this->uqcms_page(array('page' => $page, 'page_size' => $page_size, 'total' => $total['total']));
            }
            $this->assign('data', $data);
        }
        $this->display();
    }
    function notice_log_del()
    {
        $id = isset($_GET['id']) ? $_GET['id'] : showmsg('信息错误');
        $query = $this->db->delete(table('notice'), 'id = ' . $id);
        if ($query) {
            showmsg('删除成功');
        } else {
            showmsg('删除失败');
        }
    }
    function notice_log_truncate()
    {
        $query = $this->db->query("TRUNCATE TABLE " . table("notice"));
        if ($query) {
            showmsg('清除成功');
        } else {
            showmsg('清除失败');
        }
    }
    function express()
    {
        $data = $this->db->get_all("select * from " . table('api_express') . " order by id asc");
        $this->assign('data', $data);
        $this->display();
    }
    function express_edit()
    {
        $id = $_GET['id'] ? $_GET['id'] : showmsg('提交错误');
        $row = $this->db->get_one("select * from " . table('api_express') . " where id = " . $id . " order by id desc");
        if (!empty($row['param'])) {
            $row['param'] = explode(',', $row['param']);
        }
        $row['pcon'] = unserialize($row['pcon']);
        $this->assign('row', $row);
        $this->display();
    }
    function express_save()
    {
        $param = !empty($_POST['param']) ? $_POST['param'] : '';
        if (!empty($param)) {
            $setarr['pcon'] = serialize($param);
        }
        $setarr['status'] = P('status', '', '1');
        $id = P('id', false);
        if ($id) {
            $query = $this->db->update(table('api_express'), $setarr, 'id = ' . $id);
            if ($query) {
                right_json();
            } else {
                error_json('保存失败');
            }
        }
    }
    function express_ch()
    {
        $id = isset($_GET['id']) ? $_GET['id'] : showmsg('选择错误');
        $query = $this->db->query("update " . table('api_express') . " set status = (status + 1)%2 where id = " . $id . " limit 1");
        if ($query) {
            showmsg('设置成功');
        } else {
            showmsg('操作错误');
        }
    }
    function config_email()
    {
        if (!extension_loaded('openssl')) {
            $this->assign('error_status', true);
        }
        $this->display();
    }
}